package com.hfjyz.tutor.tutor.dao;

import java.util.List;

import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.dao.BaseDao;
import com.hfjyz.base.dao.PagingHibernateCallback;
import com.hfjyz.base.entity.TutorEntity;
import com.hfjyz.tutor.tutor.bean.TutorSearchBean;

public class TutorDao extends BaseDao<TutorEntity> {

    public TutorDao() {
        super(TutorEntity.class);
    }

    @SuppressWarnings("unchecked")
    public long countTutor(TutorSearchBean tutorSearch) {
        String hql = "select count(tutor.id) from TutorEntity as tutor";

        Object[] values = null;
        if (tutorSearch != null) {
            hql += tutorSearch.getWhereClause();
            List<Object> paramList = tutorSearch.getParamList();
            values = paramList.toArray();
        }
        List<Object> result = hibernateTemplate.find(hql, values);

        return (Long) result.get(0);
    }

    @SuppressWarnings("unchecked")
    public List<TutorEntity> findTutor(SearchCondition cond, TutorSearchBean tutorSearch) {
        String hql = "from TutorEntity ";

        Object[] values = null;
        if (tutorSearch != null) {
            hql += tutorSearch.getWhereClause();
            List<Object> paramList = tutorSearch.getParamList();
            values = paramList.toArray();
        }
        hql += cond.getOrderString();

        PagingHibernateCallback callback = new PagingHibernateCallback(hql, cond.getPageNum(),
                cond.getRowCountPerPage(), values);
        List<TutorEntity> result = hibernateTemplate.executeFind(callback);
        return result;
    }

    @SuppressWarnings("unchecked")
    public List<TutorEntity> findTutorWithoutPage(SearchCondition cond, TutorSearchBean tutorSearch) {
        String hql = "from TutorEntity ";

        Object[] values = null;
        if (tutorSearch != null) {
            hql += tutorSearch.getWhereClause();
            List<Object> paramList = tutorSearch.getParamList();
            values = paramList.toArray();
        }
        hql += cond.getOrderString();

        List<TutorEntity> result = hibernateTemplate.find(hql, values);
        return result;
    }

    @SuppressWarnings("unchecked")
    public TutorEntity findTutorByIdNo(String idNo) {
        String hql = "from TutorEntity e where e.idNo=?";
        List<TutorEntity> result = hibernateTemplate.find(hql, idNo);
        if (result == null || result.size() == 0) {
            return null;
        } else {
            return result.get(0);
        }
    }

    @SuppressWarnings("unchecked")
    public long countTutorByIdNo(String idNo) {
        String hql = "select count(tutor.id) from TutorEntity as tutor where idNo=?";

        List<Object> result = hibernateTemplate.find(hql, idNo);

        return (Long) result.get(0);
    }
}
